package com.hicorp.segment.controller;

import com.hicorp.segment.service.BasicMasterSubService;
import com.hicorp.segment.utils.ResultBean;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/basicMasterSubRequest/{masterTableName}")
public class BasicMasterSubController {
    private final BasicMasterSubService basicMasterSubService;

    public BasicMasterSubController(BasicMasterSubService basicMasterSubService) {
        this.basicMasterSubService = basicMasterSubService;
    }

    /**
     * 跨表查询
     * @param masterTableName 主表表名
     * @return 查询结果
     */
    @GetMapping
    public Object selectAll(@PathVariable String masterTableName) {
        return basicMasterSubService.queryInfo(masterTableName);
    }

    /**
     * 跨表新增
     * @param record          数据
     * @param masterTableName 主表表名
     * @param subTableName    子表表名
     * @param masterFiledName 主表字段名
     * @param subFiledName    子表字段名
     * @return 返还值
     */
    @PostMapping("/{subTableName}/{masterFiledName}/{subFiledName}")
    public ResultBean<Void> createRecord(@RequestBody Object record,
                                         @PathVariable String masterTableName,
                                         @PathVariable String subTableName,
                                         @PathVariable String masterFiledName,
                                         @PathVariable String subFiledName) {
        return basicMasterSubService.addRecord(record, masterTableName, subTableName, masterFiledName, subFiledName);
    }

    /**
     * 主子表修改
     * @param record 修改的记录
     * @param masterTableName 主表表名
     * @param subTableName 子表表名
     * @param masterFiledName 主表键名
     * @param subFiledName 子表键名
     * @return 修改结果(是否成功)
     */
    @PutMapping("/{subTableName}/{masterFiledName}/{subFiledName}/{id}")
    public ResultBean<Void> editRecord(@RequestBody Object record,
                                       @PathVariable String masterTableName,
                                       @PathVariable String subTableName,
                                       @PathVariable String masterFiledName,
                                       @PathVariable String subFiledName,
                                       @PathVariable Long id) {
        return basicMasterSubService.editRecord(record, masterTableName, subTableName, masterFiledName, subFiledName);
    }
}
